<?php
/**
 * Created by PhpStorm.
 * User: hepenghui
 * Date: 2018/6/13
 * Time: 上午11:22
 */

namespace common\models;

use Yii;

class PcUpdateConf extends \yii\db\ActiveRecord
{
    const ON = 1,OFF = 0;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%pc_update_conf}}';
    }

    /*public function rules()
    {
        return [
            [['is_force', 'update_version'], 'integer'],
            [['update_info', 'update_url','blacklist'], 'string'],
            [['is_force', 'update_version', 'update_version', 'update_url'], 'required', 'on' => ['update']],
        ];
    }*/

    public static function getIsForce()
    {
        return [
            self::ON => Yii::t("app","Yes"),
            self::OFF => Yii::t("app","No")
        ];
    }

    public function getData()
    {
        $data   = self::find()->asArray()->all();
        return $data;
    }

    public function backendData()
    {
        $return = [];
        $data   = $this->getData();
        foreach ($data as $item)
        {
            $return[$item["name"]]  = $item["value"];
        }
        return $return;
    }

    public function apiData()
    {
        $conf   = [];
        $list   = $this->getData();
        if(!empty($list))
        {
            foreach ($list as $item)
            {
                $data[$item["name"]]    = $item["value"];
            }
            //各项配置
            $conf["is_force"]       = $data["is_force"];
            $conf["update_version"] = $data["update_version"];
            $conf["update_url"]     = $data["update_url"];
            $conf["update_info"]    = $data["update_info"];
            $conf["blacklist"]      = empty($data["blacklist"]) ? [] : explode("|",$data["blacklist"]);
            $conf["qr_code_url"]      = $data['qr_code_url'];
        }
        return $conf;
    }
}